﻿using System;

namespace TidyBlog.Util
{
    /// <summary>
    /// 类型转换器
    /// </summary>
    public abstract class TypeConverter
    {
        /// <summary>
        /// 把字符串值转换成特定类型值
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="request">Request对象</param>
        /// <param name="value">值</param>
        /// <returns>特定类型值</returns>
        public static T ConvertTo<T>(string value)
        {
            Type type = typeof(T);
            object result;

            try
            {
                result = Convert.ChangeType(String.IsNullOrEmpty(value) ? String.Empty : value.Trim(), type);
            }
            catch
            {
                result = default(T);
            }

            return (T)result;
        }
    }
}
